package tms.util.context;

import tms.business.login.LoginService;
import tms.business.time.TimeService;
import tms.business.xml.XmlBindingService;
import tms.util.jabber.JabberManager;

import javax.ejb.EJB;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class WebContext implements ServletContextListener {
    @EJB
    XmlBindingService xmlBindingService;
    @EJB
    LoginService loginService;
    @EJB
    TimeService timeService;

    private static MailConfig mailConfig;
    private static JabberConfig jabberConfig;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        mailConfig = xmlBindingService.unmarshal(MailConfig.class, servletContextEvent.getServletContext().getResourceAsStream(Config.MAIL.file()));
        jabberConfig = xmlBindingService.unmarshal(JabberConfig.class, servletContextEvent.getServletContext().getResourceAsStream(Config.JABBER.file()));
        JabberManager.initJabberCache(loginService, timeService);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }


    public static MailConfig getMailConfig() {
        return mailConfig;
    }

    public static JabberConfig getJabberConfig() {
        return jabberConfig;
    }
}
